/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Shift;
import me.zhengjie.modules.system.service.ShiftService;
import me.zhengjie.modules.system.domain.vo.ShiftQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.Comparator;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author pysduck
* @date 2023-12-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：班次管理")
@RequestMapping("/api/shift")
public class ShiftController {

    private final ShiftService shiftService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('shift:list')")
    public void exportShift(HttpServletResponse response, ShiftQueryCriteria criteria) throws IOException {
        shiftService.download(shiftService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询系统：班次")
    @ApiOperation("查询系统：班次")
    @PreAuthorize("@el.check('shift:list')")
    public ResponseEntity<PageResult<Shift>> queryShift(ShiftQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(shiftService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增系统：班次")
    @ApiOperation("新增系统：班次")
    @PreAuthorize("@el.check('shift:add')")
    public ResponseEntity<Object> createShift(@Validated @RequestBody Shift resources){
        ShiftQueryCriteria s = new ShiftQueryCriteria();
        s.setShiftName(resources.getShiftName());
        s.setFacility(resources.getFacility());
        List<Shift> shiftList = shiftService.queryAll(s);
        if (shiftList.size() == 0) {
            resources.setShift(resources.getFacility()+resources.getShiftName()+"001");
        }else{
            String shift = shiftList.stream().collect(Collectors.toList()).stream().sorted(Comparator.comparing(Shift::getShift)).findFirst().get().getShift();
            String format = String.format("%3d", Integer.parseInt(shift.substring(-3)) + 1);
            resources.setShift(resources.getFacility()+resources.getShiftName()+format);
        }
        resources.getChildren().forEach( childShift -> {
            childShift.setFacility(resources.getFacility());
            childShift.setShift(resources.getShift());
            childShift.setShiftName(resources.getShiftName());
            childShift.setStatus(resources.getStatus());
            shiftService.create(childShift);
        });
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改系统：班次")
    @ApiOperation("修改系统：班次")
    @PreAuthorize("@el.check('shift:edit')")
    public ResponseEntity<Object> updateShift(@Validated @RequestBody Shift resources){
        List<Shift> children = resources.getChildren();
        children.forEach( o -> {
            o.setShift(resources.getShift());
            o.setShiftName(resources.getShiftName());
            o.setFacility(resources.getFacility());
            o.setStatus(resources.getStatus());
            shiftService.update(o);
        });
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除系统：班次")
    @ApiOperation("删除系统：班次")
    @PreAuthorize("@el.check('shift:del')")
    public ResponseEntity<Object> deleteShift(@RequestBody List<Integer> ids) {
        shiftService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}